package ldkxingzhe.l365calendar.adapter;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import java.util.List;

import ldkxingzhe.l365calendar.R;
import ldkxingzhe.l365calendar.model.InfoItemBase;
import ldkxingzhe.l365calendar.model.NoteEntity;
import ldkxingzhe.l365calendar.model.NoteInfoItem;
import ldkxingzhe.l365calendar.model.ScheduleEntity;
import ldkxingzhe.l365calendar.model.ScheduleInfoItem;

/**
 * 日历列表向adapter
 * Created by xingzhe on 16/2/16.
 */
public class InfoContainerAdapter extends BaseAdapter
        implements AdapterView.OnItemClickListener{
    @SuppressWarnings("unused")
    private static final String TAG = "InfoContainerAdapter";


    private List<InfoItemBase> list;

    @Override
    public int getCount() {
        if(list != null){
            return list.size();
        }
        return 0;
    }

    @Override
    public int getViewTypeCount() {
        return 2;
    }

    @Override
    public int getItemViewType(int position) {
        return getItem(position).getType();
    }

    @Override
    public InfoItemBase getItem(int position) {
        return list.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        switch (getItemViewType(position)){
            case ScheduleInfoItem.TYPE:
                getViewOfScheduleInfo(getItem(position), convertView, parent);
                break;
            case NoteInfoItem.TYPE:
                getViewOfNoteInfo(getItem(position), convertView, parent);
                break;
        }
        return convertView;
    }

    private void getViewOfScheduleInfo(InfoItemBase infoItemBase, View convertView, ViewGroup parent){
        ScheduleEntity entity = ((ScheduleInfoItem)infoItemBase).getObject();
        ViewHolderSchedule viewHolderSchedule;
        if(convertView == null){
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_info_item_note, parent, false);
            viewHolderSchedule = new ViewHolderSchedule(convertView);
            convertView.setTag(viewHolderSchedule);
        }else{
            viewHolderSchedule = (ViewHolderSchedule) convertView.getTag();
        }

        if(entity.isCompleted()){
            viewHolderSchedule.icon.setImageResource(R.drawable.info_list_icon_birth);
        }else{
            viewHolderSchedule.icon.setImageResource(R.drawable.info_list_icon_life);
        }
        viewHolderSchedule.titleText.setText(entity.getTitle());
        viewHolderSchedule.descText.setText(entity.getDescription());
        viewHolderSchedule.summaryText.setText(String.valueOf(entity.getCreated()));
    }

    private void getViewOfNoteInfo(InfoItemBase infoItemBase, View convertView, ViewGroup parent){
        NoteEntity entity = (NoteEntity) infoItemBase.getObject();
        ViewHolderNote viewHolderNote;
        if(convertView == null){
            convertView = LayoutInflater.from(parent.getContext())
                    .inflate(R.layout.list_info_item_note, parent, false);
            viewHolderNote = new ViewHolderNote(convertView);
            convertView.setTag(viewHolderNote);
        }else{
            viewHolderNote = (ViewHolderNote) convertView.getTag();
        }
        if(entity.getState() == 0){
            if(entity.getPriority() != 0){
                viewHolderNote.icon.setImageResource(R.drawable.info_list_icon_note_important);
                viewHolderNote.summaryText.setText(R.string.important_todo);
            }else{
                viewHolderNote.summaryText.setText(R.string.todo_text);
            }
            viewHolderNote.descText.setVisibility(View.GONE);
        }else{
            viewHolderNote.icon.setImageResource(R.drawable.info_list_icon_note_done);
            viewHolderNote.descText.setVisibility(View.VISIBLE);
        }
        viewHolderNote.titleText.setText(entity.getTitle());
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Object object = getItem(position).getObject();
        switch (getItemViewType(position)){
            case NoteInfoItem.TYPE:
                onNoteItemClick((NoteEntity)object);
                break;
            case ScheduleInfoItem.TYPE:
                onScheduleItemClick((ScheduleEntity)object);
                break;
            default:
                throw new IllegalStateException("view type is wrong, " + getItemViewType(position));
        }
    }

    private void onScheduleItemClick(ScheduleEntity entity) {

    }

    private void onNoteItemClick(NoteEntity entity) {

    }

    private class ViewHolderSchedule extends ViewHolderNote{

        public ViewHolderSchedule(View convertView) {
            super(convertView);
        }
    }

    private class ViewHolderNote{
        final ImageView icon;
        final TextView descText, summaryText, titleText;
        public ViewHolderNote(View convertView){
            icon = (ImageView) convertView.findViewById(R.id.icon);
            descText = (TextView) convertView.findViewById(R.id.desc);
            summaryText = (TextView) convertView.findViewById(R.id.summary);
            titleText = (TextView) convertView.findViewById(R.id.title);
        }
    }
}
